﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Fusion.Content
{
    public class GameResource
    {
        // Data Members
        protected object Data;
        protected int RefCount;

        // Member Properties
        public int ReferenceCount
        {
            get { return RefCount; }
        }
        public bool IsNullResource
        {
            get { return (Data == null); }
        }
        public int Handle
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }

        // Constructor
        public GameResource() 
        {
            if (this != null)
            {
                RefCount = 0;
                Release();
            }

            RefCount = 1;
        }

        // Public Methods
        public virtual void AddRef()
        {
            ++RefCount;
        }
        public virtual void Release(bool GarbageCollect = false)
        {
            --RefCount;

            if (RefCount <= 0)
                GameResourceManager.ReleaseObject(this, GarbageCollect);
        }

        // Used for Releasing Objects
        private static class GameResourceManager
        {
            public static void ReleaseObject(GameResource Object, bool GarbageCollect = false)
            {
                Object = null;

                if (GarbageCollect)
                    GC.Collect();
            }
        }
    }
}
